package cn.dengta.webapp.trade.entity;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import cn.dengta.common.web.Client;
import cn.dengta.webapp.base.entity.BizEntity;
import cn.dengta.webapp.trade.model.*;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.StringUtil;
import me.codeplayer.util.X;

/**
 * 充值商品
 *
 * @author YUQIDI
 * @since 2022-03-21
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName(value = "tr_recharge_product", autoResultMap = true)
public class RechargeProduct extends BizEntity {

	public static String STD_CODE = "com.qs.flala";
	public static String IOS_CODE = "com.sheyu.fla";
	public static String diamonds = "diamonds";
	public static String member = "member";
	public static String SEP = ".";

	public static Map<String, Map<String, String>> BUNDLE_ID_MAP = Map.of(
			"iOS", Map.of("main", IOS_CODE, "vest1", "com.dtkj.tooyou"),
			"Android", Map.of("main", STD_CODE)
	);

	/** 第三方支付的内购商品代号 */
	private String code;
	/**
	 * 产品类型 type=5时  参考
	 *
	 * @see cn.dengta.webapp.trade.model.GroupVipLevel
	 */
	private ProductType productType;
	/** 虚拟币 */
	private Integer coin;
	/** 免费赠送的钻石数（不含VIP免费赠送数） */
	private Integer freeCoin;
	/** 如果用户是VIP，额外赠送的虚拟币 */
	private Integer vipFreeCoin;
	/** 价格 */
	private BigDecimal price;
	/** 币种 */
	private String currency;
	/** 支付业务类型：1=充值钻石；2=个人VIP充值；3=购买提升搭讪推荐曝光率服务；4=积分兑换；5=群VIP充值 @see  PayType */
	private Integer type;
	/**
	 * 当type=5时可选值
	 *
	 * @see cn.dengta.webapp.trade.model.GroupVipLevel
	 * 当type=充值时
	 * 0=女；1=男
	 */
	private Integer subType;
	/** VIP天数 */
	protected Integer vipDays;
	/**
	 * 客户端类型：2=Android；3=iOS
	 *
	 * @see cn.dengta.common.web.Client
	 */
	private Client client;
	/** 商品标签（例如：30% off、连续包月） */
	private String tag;
	/** 人性化提示信息（例如：折合 $0.99/月 ） */
	private String tips;
	/** 排序 */
	private Integer sort;
	/** VIP配置的JSON数据  当type=5时 存tr_group_vip_equity表的id */
	private String vipConfig;
	/** 适用范围0=未知；1=国外；2=国内 */
	private Integer scope;
	/** 分包处理 */
	private Integer wesKit;
	/** 渠道 */
	private String channel;
	/** 图标 */
	private String icon;

	/** 获取权益ID */
	public Long findEquityId() {
		if (!PayType.GROUP_VIP.getValue().equals(this.type)) {
			return null;
		}
		if (X.isValid(vipConfig)) {
			GroupVipConfig config = JSONObject.parseObject(this.vipConfig, GroupVipConfig.class);
			return config.getEquityId();
		}
		return null;
	}

	/** 获取群VIP 配置数据 */
	public GroupVipConfig findGroupVipConfig() {
		if (!PayType.GROUP_VIP.getValue().equals(this.type)) {
			return null;
		}
		if (X.isValid(vipConfig)) {
			return JSONObject.parseObject(this.vipConfig, GroupVipConfig.class);
		}
		return null;
	}

	/** 内购商品 优惠枚举 */
	private transient RechargeDiscount rechargeDiscount;

	public RechargeProduct init(RechargeProduct input, Date now) {
		this.code = X.expectNotNull(input.code, code);
		this.coin = X.expectNotNull(input.coin, coin);
		this.freeCoin = X.expectNotNull(input.freeCoin, freeCoin);
		this.vipFreeCoin = X.expectNotNull(input.vipFreeCoin, vipFreeCoin, 0);
		this.price = X.expectNotNull(input.price, price);
		this.currency = X.expectNotNull(input.currency, currency);
		this.type = X.expectNotNull(input.type, type);
		this.vipDays = X.expectNotNull(input.vipDays, vipDays);
		this.client = X.expectNotNull(input.client, client);
		this.tag = X.expectNotNull(input.tag, tag);
		this.tips = X.expectNotNull(input.tips, tips);
		this.sort = X.expectNotNull(input.sort, sort, 0);
		this.vipConfig = StringUtil.toString(input.vipConfig);
		this.productType = X.expectNotNull(input.productType, productType);
		this.scope = X.expectNotNull(input.scope, scope);
		this.icon = X.expectNotNull(input.icon, icon);
		this.wesKit = X.expectNotNull(input.wesKit, wesKit);
		super.initTime(now);
		super.init(input.status, input.state);
		return this;
	}

	public boolean isSTD() {
		return productType == ProductType.STD;
	}

	public static String mainCode(Client client) {
		return client == Client.APP_ANDROID ? STD_CODE : IOS_CODE;
	}

	public boolean isVar() {
		return productType == ProductType.VAR;
	}

	/** 可动态配置 */
	public boolean isAmountAsDynamic() {
		return productType == ProductType.VAR;
	}

	public static final String CODE = "code";
	public static final String TYPE = "type";
	public static final String PRODUCT_TYPE = "product_type";
	public static final String SUB_TYPE = "sub_type";
	public static final String CLIENT = "client";
	public static final String COIN = "coin";
	public static final String FREE_COIN = "free_coin";
	public static final String VIP_FREE_COIN = "vip_free_coin";
	public static final String PRICE = "price";
	public static final String CURRENCY = "currency";
	public static final String VIP_DAYS = "vip_days";
	public static final String TAG = "tag";
	public static final String TIPS = "tips";
	public static final String SORT = "sort";
	public static final String VIP_CONFIG = "vip_config";
	public static final String SCOPE = "scope";
	public static final String WES_KIT = "wes_kit";
	public static final String CHANNEL = "channel";

	@Setter
	@Getter
	public static class GroupVipConfig {

		/** 权益id */
		private Long equityId;
		/** 单位 天=d, 月=m */
		private String unit;
		/** 具体的天数或者月数 */
		private Integer number;

	}

}
